<?php
class Page_Model_Message_Collection {

	const SESSION_VAR_NAME = 'messages';
	
	protected $_messages = array();
	
	public function __construct() {
		$sessionMsg = $this->_getSession()->get(self::SESSION_VAR_NAME);
		$this->_messages = is_array($sessionMsg) ? $sessionMsg : array();
	}
	
	protected function _addMessage($type, $s) {
		$this->_messages[] = new Page_Model_Message($type, $s);
		$this->_getSession()->set(self::SESSION_VAR_NAME, $this->_messages);
	}
	
	protected function _getSession() {
		return Mag::singleton('Core_Model_Session');
	}

	public function clear() {
		$this->_getSession()->set(self::SESSION_VAR_NAME, null);
		$this->_messages = array();
	}

	public function getMessages() {
		$result = $this->_messages;
		$this->clear();
		return $result;
	}

	public function addErrors($errors) {
		foreach($errors as $e)
			$this->addError($e);
	}

	public function addError($s) {
		$this->_addMessage(Page_Model_Message::ERROR, $s);
	}
	
	public function addSuccess($s) {
		$this->_addMessage(Page_Model_Message::SUCCESS, $s);
	}
	
}